/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.project.dao.FrontendDao;
import io.renren.modules.project.entity.FrontendEntity;
import io.renren.modules.systemconfig.entity.ProjectBaseEntity;
import io.renren.modules.project.service.FrontendService;
import io.renren.modules.systemconfig.service.ProjectBaseService;
import io.renren.modules.systemconfig.service.SysConfigServerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service("frontendService")
public class FrontendServiceImpl extends ServiceImpl<FrontendDao, FrontendEntity> implements FrontendService {

	@Autowired
	private ProjectBaseService projectBaseService;

	@Autowired
	private SysConfigServerService systemConfigServerService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String key = (String)params.get("key");
		Long  current = Long.parseLong((String)params.get("page"));
		Long size = Long.parseLong((String)params.get("limit"));
		List<FrontendEntity> records = this.baseMapper.selectEntityPage(key,size*(current-1),size*current);
		IPage<FrontendEntity> page = new Page<>(current, size, this.baseMapper.selectRecordSize(key));
		page.setRecords(records);
		return new PageUtils(page);
	}


	@Override
	public FrontendEntity queryByKey(String key) {
		return baseMapper.queryByKey(key);
	}

	@Override
	@Transactional
	public void saveEntity(FrontendEntity entity) {
		this.baseMapper.insert(entity);
	}

	@Override
	@Transactional
	public void update(FrontendEntity user) {
		this.updateById(user);
	}

	@Override
	public void deleteBatch(Long[] userId) {
		this.removeByIds(Arrays.asList(userId));
	}

	@Override
	public FrontendEntity getAllInfoById(long frontId) {
		FrontendEntity fe=this.baseMapper.selectById(frontId);
		fe.setProject(projectBaseService.getById(fe.getProjectId()));
		fe.setServer(systemConfigServerService.getById(fe.getServerId()));
		return fe;
	}

	@Override
	public List<FrontendEntity> getListByServerId(Long id) {
		return this.baseMapper.selectList(new QueryWrapper<FrontendEntity>().eq("server_id",id));
	}


}